package com.pason.onlineshop.mainshop.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.pason.onlineshop.mainshop.controller.form.OrderDTO;
import com.pason.onlineshop.mainshop.domain.Order;
import com.pason.onlineshop.mainshop.service.entity.IOrderEntityService;

@Controller
public class OrderRESTController {

	private static final Logger logger = LoggerFactory.getLogger(OrderRESTController.class);
	
	@Autowired(required=true)
	private IOrderEntityService orderService;
	
	@RequestMapping(value = "/order", method=RequestMethod.GET)
	public String listOrder() {
	  logger.debug("~~~ listOrder ~~~");
	  List<Order> orders = orderService.listOrders();
	  return "order"; 
	}
	
	@RequestMapping(value = "/order/{id}", method=RequestMethod.GET)
	@ResponseBody
	public Order getOrder(@PathVariable Long id) {
      logger.debug("~~~ getOrder ~~~");
	  Order order = orderService.findById(id);
	  return order; 
	}
	
	@RequestMapping(value = "/order/{id}", method=RequestMethod.DELETE)
	@ResponseStatus(HttpStatus.OK)
	public void deleteOrder(@PathVariable Long id) {
	  logger.debug("~~~ deleteOrder ~~~");
	  orderService.deleteOrder(id);
	}
	
	@RequestMapping(value = "/order", method=RequestMethod.PUT)
	@ResponseBody
	@ResponseStatus(HttpStatus.OK)
	public Order submitOrder(OrderDTO orderDTO) {
		logger.debug("~~~ submitOrder ~~~");
		Order order = orderService.saveOrder(Order.fromDTO(orderDTO));
		return order;
	}
	
}
